import 'package:flutter/material.dart';
import 'package:interview/theme/app_colors.dart';

class PageLoadOverlay {
  static OverlayEntry? _overlayEntry;

  /// 显示加载框
  static void show({
    required BuildContext context,
    double iconSize = 36.0, // 图标大小
    double padding = 24.0, // 图标四周的 padding
    bool? isProgress = true,
  }) {
    // 如果已经显示，避免重复添加
    if (_overlayEntry != null) return;

    // 获取 OverlayState
    final overlayState = Overlay.of(context);

    // 获取主题中的 PageLoadOverlay 样式
    final theme = Theme.of(context).extension<AppColors>()!;
    final backgroundColor = theme.contrastPure;
    final loadingColor = theme.subjectPure;

    // 创建 OverlayEntry
    _overlayEntry = OverlayEntry(
      builder: (BuildContext context) {
        return Material(
          color: Colors.transparent,
          child: Center(
            child:  Container(
            padding: EdgeInsets.all(padding),
            decoration: BoxDecoration(
              color: backgroundColor,
              borderRadius: BorderRadius.circular(12.0),
            ),
            child: !isProgress! ? SizedBox(
              width: iconSize,
              height: iconSize,
              child: CircularProgressIndicator(
                strokeWidth: 1.0,
                color: loadingColor,
              ),
            )
          : SizedBox(
              width: iconSize * 1.2,
              height: 2,
              child: LinearProgressIndicator(
                value: null,
                backgroundColor: backgroundColor,
                valueColor: AlwaysStoppedAnimation<Color>(loadingColor!),
              ),
            ),
          ),
          ),
        );
      },
    );

    // 显示 OverlayEntry
    overlayState.insert(_overlayEntry!);
  }

  /// 隐藏加载框
  static void hide() {
    _overlayEntry?.remove();
    _overlayEntry = null;
  }
}
